import java.util.InputMismatchException;
import java.util.Scanner;

class Person implements Cloneable{
    String name;

    int age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static void main3(String[] args) {
//        System.out.println(10/0);
        int[] array = {1,2,3,4,5};
        System.out.println(array[20]);
    }

    public static void main4(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person) person1.clone();
    }

    public static void testException(int a) {
        if(a == 0) {
            throw new ArithmeticException("a == 0");
        }
    }

    public static void main5(String[] args) {
        testException(0);
    }

    public static void function(int a) throws CloneNotSupportedException {
        if(a == 10) {
            throw new CloneNotSupportedException();
        }
    }
    public static void main6(String[] args) {
        try {
            function(10);
        } catch (CloneNotSupportedException e) {
            System.out.println("处理不支持克隆异常");
            e.printStackTrace();
        }

        System.out.println("程序正常运行");
    }

    public static void main7(String[] args) {
        int[] array = {1,2,3};

        try {
            System.out.println(array[20]);
            array = null;
            System.out.println(array.length);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("处理数组下标越界异常");
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("处理空指针异常");
            e.printStackTrace();
        }

        System.out.println("程序正常运行");
    }

    public static void main8(String[] args) {
        int[] array = {1,2,3};

        try {
            System.out.println(array[21]);
            array = null;
            System.out.println(array.length);
        } catch (NullPointerException e) {
            System.out.println("处理空指针异常");
            e.printStackTrace();
        }

        System.out.println("程序正常运行");
    }

    //演示finally关键字
    public static void main9(String[] args) {
        int[] array = {1,2,3};

        try {
            System.out.println(array[21]);
            array = null;
            System.out.println(array.length);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界！");
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        }
        finally {
            System.out.println("finally被执行了");
        }

        System.out.println("程序正常结束");
    }

    //try with resource 语句：在结束时自动关闭资源

    public static int getData() {
        try(Scanner sc = new Scanner(System.in)) {
            int data = sc.nextInt();
            return data;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally 中代码");
        }

        System.out.println("try-catch-finally之后");
        return 0;
    }

    public static void main10(String[] args) {
        //finally 一般用于资源的释放
        int data = getData();
        System.out.println(data);
    }

    public static int testFinally() {
        try {
            return 10;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally中代码");
        }

        System.out.println("try-catch-finally之后");
        return 0;
    }

    public static void main11(String[] args) {
        System.out.println(testFinally());
    }

    public static void test1() {
        int[] array = {1,2,3,4};
        System.out.println(array[10]);
    }

    public static void main12(String[] args) {
        try {
            test1();
            //如果test1()抛出异常下面的代码就不会执行
            System.out.println("结束try的代码");
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }

        System.out.println("try-catch之后的代码");
    }




}
